package com.futures.questionbank.my;

import android.text.TextUtils;
import android.widget.EditText;

import com.allen.library.SuperButton;
import com.futures.questionbank.R;
import com.futures.questionbank.Utils.SPUtil;
import com.futures.questionbank.Utils.UItils;
import com.futures.questionbank.base.BaseActivity;
import com.futures.questionbank.db.table.User;
import com.futures.questionbank.db.table.User_Table;
import com.raizlabs.android.dbflow.sql.language.Select;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author : huang
 * date   : 2020/6/16 0016 11:09
 * version: 1.0
 */
public class ChangePassWordActivity extends BaseActivity {
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.sb_ok)
    SuperButton sbOk;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.change_password_activity);
        setTitle("修改密码");

    }

    @Override
    protected void loadData() {

    }


    @OnClick(R.id.sb_ok)
    public void onViewClicked() {
        if (TextUtils.isEmpty(etPhone.getText().toString().trim())) {
            UItils.showToastSafe(etPhone.getHint().toString());
            return;
        }
        if (TextUtils.isEmpty(etCode.getText().toString().trim())) {
            UItils.showToastSafe(etCode.getHint().toString());
            return;
        }
        if (TextUtils.isEmpty(etPassword.getText().toString().trim())) {
            UItils.showToastSafe(etPassword.getHint().toString());
            return;
        }
        if (!TextUtils.equals(etCode.getText().toString().trim(),etPassword.getText().toString().trim())){
            UItils.showToastSafe("2次输入密码不一致");
            return;
        }
        User user = new Select()
                .from(User.class)
                .where(User_Table.phone.is(SPUtil.getString("phone")))
                .querySingle();

         if (null != user){
             if (!user.getPassword().equals(etPhone.getText().toString().trim())){
                 UItils.showToastSafe("原密码输入错误");
                 return;
             }
             user.setPassword(etPassword.getText().toString().trim());
             user.update();
             UItils.showToastSafe("修改密码成功");
             finish();
         }
    }
}
